package vn.bus.bk;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;

import vn.bus.bk.map.BusMap;
import vn.bus.bk.object.BusInfo;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewInfoBus extends SherlockFragmentActivity {

    public static BusInfo myBus;
    private TextView numberBus, timeStartBus, timeEndBus, goBus, backBus,
	    busValue;
    private ImageView share, map;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.viewinfobus);
	setTitle(myBus.getName());
	getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	getSupportActionBar().setHomeButtonEnabled(true);
	init();
	setup();
    }

    private void init() {
	// TODO Auto-generated method stub
	numberBus = (TextView) findViewById(R.id.tvNumberBus);
	timeStartBus = (TextView) findViewById(R.id.tvTimeStartBus);
	timeEndBus = (TextView) findViewById(R.id.tvTimeEndBus);
	goBus = (TextView) findViewById(R.id.tvGoBus);
	backBus = (TextView) findViewById(R.id.tvBackBus);
	busValue = (TextView) findViewById(R.id.tvBusValue);
	
	share = (ImageView) findViewById(R.id.img_share);
	map = (ImageView) findViewById(R.id.img_view_map);
	map.setEnabled(false);
	map.setImageResource(R.drawable.map_view_disable);
	if(myBus.getOrdinateGo() != null){
	    if(myBus.getOrdinateGo().trim().length() != 0){
		map.setEnabled(true);
		map.setImageResource(R.drawable.view_map);
	    }
	}
	
	share.setOnClickListener(new OnClickListener() {
	    
	    @Override
	    public void onClick(View v) {
		loginFacebookAndShare(myBus);
	    }
	});
	
	map.setOnClickListener(new OnClickListener() {
	    
	    @Override
	    public void onClick(View v) {
		BusMap.myBus = myBus;
		startActivity(new Intent(ViewInfoBus.this, BusMap.class));		
	    }
	});

    }

    private void setup() {
	setTitle(myBus.getName());
	numberBus.setText(" " + String.valueOf(myBus.getNumber()));
	timeStartBus.setText(" " + myBus.getTimeStart());
	timeEndBus.setText(" " + myBus.getTimeEnd());
	goBus.setText(" " + myBus.getBusGo());
	backBus.setText(" " + myBus.getBusBack());
	if (myBus.getValue() != null) {
	    if (myBus.getValue().trim().length() != 0) {
		busValue.setText(" " + myBus.getValue());
	    } else {
		busValue.setText(" Updating");
	    }
	} else {
	    busValue.setText(" Updating");
	}
	

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	if (item.getItemId() == android.R.id.home) {
	    finish();
	}
	return super.onOptionsItemSelected(item);
    }
    
    @SuppressWarnings({ "deprecation"})
    private void loginFacebookAndShare(final BusInfo myBus) {
	final Facebook facebook = new Facebook(getString(R.string.app_id));

	sp = getPreferences(0);
	String access_token = sp.getString("access_token", null);
	long expire = sp.getLong("access_expire", 0);
	if (access_token != null) {
	    facebook.setAccessToken(access_token);
	}
	if (expire != 0) {
	    facebook.setAccessExpires(expire);
	}
	if (facebook.isSessionValid()) {
	    postToFacebook(myBus, facebook);
	} else {
	    Log.i("STATE", "LOGIN");

	    String[] permissions = { "" };
	    facebook.authorize(this, permissions, android.widget.Adapter.IGNORE_ITEM_VIEW_TYPE,
		    new DialogListener() {

			@Override
			public void onFacebookError(FacebookError e) {
			    // TODO Auto-generated method stub

			}

			@Override
			public void onError(DialogError e) {
			    // TODO Auto-generated method stub

			}

			@SuppressLint("CommitPrefEdits")
			@Override
			public void onComplete(Bundle values) {
			    // TODO Auto-generated method stub
			    postToFacebook(myBus, facebook);
			    Editor editor = sp.edit();
			    editor.putString("access_token",
				    facebook.getAccessToken());
			    editor.putLong("access_expire",
				    facebook.getAccessExpires());
			    editor.commit();
			}

			@Override
			public void onCancel() {
			    // TODO Auto-generated method stub

			}
		    });
	}

    }

    @SuppressWarnings("deprecation")
    private void postToFacebook(BusInfo myBusShare, Facebook facebook) {

	Bundle paramas = new Bundle();
	paramas.putString("caption", "Xe " + myBusShare.getNumber() + " : "
		+ myBusShare.getName());
	paramas.putString("description", myBusShare.getBusGo());
	paramas.putString("link",
		"https://www.facebook.com/pages/Bus-H%C3%A0-N%E1%BB%99i/157046531109544");
	paramas.putString("picture",
		"http://baodientu.chinhphu.vn/Uploaded_VGP/hatheluc/20120410/xe-bus-ha-noi.jpg");

	facebook.dialog(this, "feed", paramas, new DialogListener() {

	    public void onFacebookError(FacebookError e) {
		// TODO Auto-generated method stub

	    }

	    public void onError(DialogError e) {
		// TODO Auto-generated method stub

	    }

	    public void onComplete(Bundle values) {
		// TODO Auto-generated method stub

	    }

	    public void onCancel() {
		// TODO Auto-generated method stub

	    }
	});
    }

}
